#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System;
using ZO.SmartCore.Security.Cryptography;
using ZO.SmartCore.Xml;

#endregion

namespace ZO.SmartCore.Protocols.XMPP.Protocol.IQ
{
    /// <summary>
    /// Represent an auth query
    /// </summary>
    public class Auth : Element
    {

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="Auth"/> class.
        /// </summary>
        public Auth()
            : base("query", "", "", "", UriList.AuthQuery)
        {
        }
        #endregion

        #region Destructor

        #endregion

        #region Fields

        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the digest for this AuthQuery.
        /// </summary>
        /// <value>returns <see langword="null" /> if the digest has not been set.</value>
        public string Digest
        {
            get
            {
                return GetChildElementValue("digest");
            }
            set
            {
                if (String.IsNullOrEmpty(value))
                {
                    base.RemoveChildElement("digest");
                }
                else
                {
                    SetChildElementValue("digest", value);
                }
            }
        }

        /// <summary>
        /// Gets or sets the resource for this AuthQuery.
        /// </summary>
        /// <value>returns <see langword="null" /> if the resource has not been set.</value>
        public string Resource
        {
            get
            {
                return GetChildElementValue("resource");
            }
            set
            {
                if (String.IsNullOrEmpty(value))
                {
                    base.RemoveChildElement("resource");
                }
                else
                {
                    SetChildElementValue("resource", value);
                }
            }
        }

        /// <summary>
        /// Gets or sets the username for this AuthQuery.
        /// </summary>
        /// <value>returns <see langword="null" /> if the username has not been set.</value>
        public string Username
        {
            get
            {
                return GetChildElementValue("username");
            }
            set
            {
                if (String.IsNullOrEmpty(value))
                {
                    base.RemoveChildElement("username");
                }
                else
                {
                    SetChildElementValue("username", value);
                }
            }
        }

        /// <summary>
        /// Gets or sets the password for this AuthQuery.
        /// </summary>
        /// <value>returns <see langword="null" /> if the password has not been set.</value>
        public string Password
        {
            get
            {
                return GetChildElementValue("password");
            }
            set
            {
                if (String.IsNullOrEmpty(value))
                {
                    base.RemoveChildElement("password");
                }
                else
                {
                    SetChildElementValue("password", value);
                }
            }
        }
 

        #endregion

        #region Methods

        /// <summary>
        /// Sets the auth plain.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        public void SetAuthPlain(string username, string password)
        {
            base.RemoveChildElement("digest");
            this.Username = username;
            this.Password = password;
        }

        /// <summary>
        /// Sets the auth digest.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <param name="streamID">The stream ID.</param>
        public void SetAuthDigest(string username, string password, string streamID)
        {
            base.RemoveChildElement("password");
            this.Username = username;
            this.Digest = Cryptographer.CreateHash(HashAlgorithm.SHA1, streamID + password);
        }

 



        #endregion
    }
}
